﻿package net.wangit.jdbc.executable;

public class Select {

	private String selectClause;
	private String fromClause;
	private String whereClause;
	private String orderByClause;

	public Select() {
	}

	public String toStatementString() {
		StringBuffer buf = new StringBuffer(selectClause.length()
				+ fromClause.length() + whereClause.length() + 20);
		buf.append("select ").append(selectClause).append(" from ").append(
				fromClause).append(" where ").append(whereClause);
		if (orderByClause != null && orderByClause.trim().length() > 0)
			buf.append(" order by ").append(orderByClause);
		return buf.toString();
	}

	public Select setFromClause(String fromClause) {
		this.fromClause = fromClause;
		return this;
	}

	public Select setFromClause(String tableName, String alias) {
		fromClause = (new StringBuilder(String.valueOf(tableName))).append(' ')
				.append(alias).toString();
		return this;
	}

	public Select setOrderByClause(String orderByClause) {
		this.orderByClause = orderByClause;
		return this;
	}

	public Select setSelectClause(String selectClause) {
		this.selectClause = selectClause;
		return this;
	}

	public Select setWhereClause(String whereClause) {
		this.whereClause = whereClause;
		return this;
	}
}
